package cn.edu.tsinghua.ie.ieFunds.repository;

import cn.edu.tsinghua.ie.ieFunds.bean.base.Stock;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.Table;
import java.util.List;

public interface StockRepository extends JpaRepository<Stock, Long>, JpaSpecificationExecutor<Table> {

    Page<Stock> findAllByName(String name, Pageable pageable);

    @Query("select s1 from Stock s1 where not exists(select 1 from Stock s2 where s2.name = s1.name and s2.time > s1.time)")
    Page<Stock> getDistinctPage(Pageable pageable);

    @Query("select s1 from Stock s1 where not exists(select 1 from Stock s2 where s2.name = s1.name and s2.time > s1.time)")
    List<Stock> getDistinctList();
}
